package com.thinkgem.jeesite.modules.wechat.controller;

import com.thinkgem.jeesite.common.utils.CacheUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
@Controller
@RequestMapping(value = "${wechatPath}/rest")
public class WeChatController extends BaseController {
	private static final String WECHAT_USER_CACHE = "wechatUserCache";
	/**
	 * 微信认证后，api测试地址
	 * 
	 * @return
	 */
	@RequestMapping(value = "/getWechatToken", method = RequestMethod.GET)
	public WxMpUser getWechatToken(HttpServletRequest request, HttpServletResponse response) {

		String openId = request.getHeader("openid");

		if (StringUtils.isNotBlank(openId) && CacheUtils.get(WECHAT_USER_CACHE, openId) != null) {
			return (WxMpUser)CacheUtils.get(WECHAT_USER_CACHE, openId);
		}
		
		return null;
	}
}